package complex.web;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import complex.dto.Player;
import complex.services.PlayersService;

@Controller
public class SearchController {

	private PlayersService playersService;

	public void setPlayersService(PlayersService playersService) {
		this.playersService = playersService;
	}

	@RequestMapping("search")
	public ModelAndView search(@RequestParam(value = "search", required = false) String query) {
		if (query == null) {
			return new ModelAndView("search");
		} else {
			List<Player> players = playersService.searchPlayers(query);
			ModelAndView mv = new ModelAndView();
			mv.setViewName("search");
			mv.addObject("players", players);
			return mv;
		}
	}
}
